window.tywx || (window.tywx = {}),
    tywx.TCPClient = {
        TAG: "TCP client",
        CONNECT_STATUS_OK: 1,
        CONNECT_STATUS_OPENING: 2,
        CONNECT_STATUS_CLOSING: 3,
        CONNECT_STATUS_FAIL: 0,
        connectStatus: 0,
        isTimerInited: !1,
        tickCount: 0,
        filterCmds: ["heart_beat"],
        timerSchedule: function () {
            tywx.TCPClient.tickCount = (tywx.TCPClient.tickCount + 1) % 3, 2 == tywx.TCPClient.tickCount && tywx.TCPClient.connectStatus == tywx.TCPClient.CONNECT_STATUS_OK && tywx.NotificationCenter.trigger(tywx.EventType.SEND_HEART_BEAT),
                tywx.TCPClient.reconnet();
        },
        startCheckTimer: function () {
            tywx.TCPClient.isTimerInited = !0, setInterval(this.timerSchedule, 1e3);
        },
        stopCheckTimer: function () {
            tywx.TCPClient.isTimerInited = !1, clearInterval(this.timerSchedule);
        },
        connect: function (e) {
            tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeTCPStart, [e]),
            tywx.TCPClient.connectStatus != tywx.TCPClient.CONNECT_STATUS_OPENING && tywx.TCPClient.connectStatus != tywx.TCPClient.CONNECT_STATUS_OK && (tywx.TCPClient.connectStatus = tywx.TCPClient.CONNECT_STATUS_OPENING,
            tywx.IsWechatPlatform() && this.doWechatConnect(e));
        },
        doWechatConnect: function (e) {
            try {
                if (!tywx.IsWechatPlatform()) return;
                wx.connectSocket({
                    url: e
                }), wx.onSocketOpen(function (t) {
                    tywx.LOGD(tywx.TCPClient.TAG, "TCP webSocket opened..."), tywx.TCPClient.connectStatus = tywx.TCPClient.CONNECT_STATUS_OK,
                        tywx.NotificationCenter.trigger(tywx.EventType.TCP_OPENED), tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeTCPSuccess, [e]),
                    tywx.TCPClient.isTimerInited || tywx.TCPClient.startCheckTimer();
                }), wx.onSocketError(function (t) {
                    tywx.LOGD(tywx.TCPClient.TAG, "TCP webSocket error..."), tywx.BiLog.clickStat(tywx.clickStatEventType.clickStatEventTypeTCPFailed, [e]),
                        tywx.TCPClient.connectStatus = tywx.TCPClient.CONNECT_STATUS_FAIL, tywx.NotificationCenter.trigger(tywx.EventType.TCP_ERROR);
                }), wx.onSocketClose(function (e) {
                    tywx.LOGD(tywx.TCPClient.TAG, "WebSocket 已关闭！"), tywx.TCPClient.connectStatus = tywx.TCPClient.CONNECT_STATUS_FAIL,
                        tywx.NotificationCenter.trigger(tywx.EventType.TCP_CLOSE);
                }), wx.onSocketMessage(function (e) {
                    if (tywx.StateInfo.isOnForeground) {
                        var t = tywx.TCPClient.decodeMessage(e.data);
                        if (null != t && "0000" != t) {
                            var n = "[Receive TCP Msg]: " + unescape(t.replace(/\\u/gi, "%u")),
                                o = t.substr(0, t.length - 0);
                            if (null != o && o.length > 0) {
                                var i = JSON.parse(o);
                                -1 == tywx.TCPClient.filterCmds.indexOf(i.cmd) && tywx.LOGD(tywx.TCPClient.TAG, n),
                                    tywx.NotificationCenter.trigger(tywx.EventType.TCP_RECEIVE, i);
                            }
                        }
                    }
                });
            } catch (e) {
                tywx.LOGE("error:", "tywx.TCPClient.doWechatConnect——" + JSON.stringify(e));
            }
        },
        decodeMessage: function (e) {
            if ("undefined" != typeof ArrayBuffer && e instanceof ArrayBuffer) {
                for (var t = new Uint8Array(e), n = "", o = 0, i = t.length; o < i; o++) {
                    n += String.fromCharCode(t[o]);
                }
                return n;
            }
            var a = (e = tywx.EncodeDecode.base64Decode(e)).slice(0, 4);
            for (o = 0, i = (e = e.slice(4)).length; o < i; o++) {
                var r = e[o];
                r ^= a[o % 4], e[o] = r;
            }
            return tywx.EncodeDecode.utf8Decode(e);
        },
        reconnet: function () {
            tywx.StateInfo.isOnForeground && tywx.TCPClient.connectStatus == tywx.TCPClient.CONNECT_STATUS_FAIL && (tywx.NotificationCenter.trigger(tywx.EventType.TCP_RECONNECT),
                tywx.TCPClient.connect(tywx.SystemInfo.webSocketUrl));
        },
        sendMsg: function (e) {
            try {
                if (tywx.TCPClient.connectStatus != tywx.TCPClient.CONNECT_STATUS_OK) return;
                var t = JSON.stringify(e);
                -1 == tywx.TCPClient.filterCmds.indexOf(e.cmd) && tywx.LOGD(tywx.TCPClient.TAG, "TCP sendMsg:" + t),
                tywx.IsWechatPlatform() && wx.sendSocketMessage({
                    data: t,
                    success: function (e) {
                        tywx.LOGD(tywx.TCPClient.TAG, "TCP sendMsg success:" + JSON.stringify(e));
                    },
                    fail: function (e) {
                        var t = e[0];
                        t && "sendSocketMessage:fail taskID not exist" === t.errMsg && (wx.closeSocket(),
                            tywx.TCPClient.connectStatus = tywx.TCPClient.CONNECT_STATUS_FAIL), tywx.LOGD(tywx.TCPClient.TAG, "TCP sendMsg fail:" + JSON.stringify(arguments));
                    },
                    complete: function (e) {
                    }
                });
            } catch (e) {
                tywx.LOGE("error:", "tywx.TCPClient.sendMsg——" + JSON.stringify(e));
            }
        },
        close: function () {
            try {
                tywx.TCPClient.connectStatus = tywx.TCPClient.CONNECT_STATUS_CLOSING, tywx.IsWechatPlatform() && wx.closeSocket(),
                    tywx.TCPClient.stopCheckTimer(), tywx.LOGD(tywx.TCPClient.TAG, "TCP close..............");
            } catch (e) {
                tywx.LOGE("error:", "tywx.TCPClient.close——" + JSON.stringify(e));
            }
        }
    }
